<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
  </head>
  <body>

    <div class="layui-fluid">
      <div class="layui-card" style="padding-bottom: 100px;">

        <div class="layui-card-body">
          <script type="text/html" id="toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
        </script>
          <table id="dataTable" lay-filter="dataTable"></table>
          <script type="text/html" id="switchTpl">
            <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="sexDemo" {{ d.status == 1 ? "checked" : "" }}>
        </script>
          <script type="text/html" id="options">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="look">权限</button>
                <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="edit">编辑</button>
                <!-- <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del"><i class="layui-icon layui-icon-edit"></i>删除</button> -->
        </script>
        </div>
      </div>
    </div>

    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
    <script>
      layui.use(['table', 'form', 'jquery','common','laydate'], function() {
              let table = layui.table;
                      let form = layui.form;
              let laydate = layui.laydate;
              let $ = layui.jquery;
              let common = layui.common;
                       laydate.render({ 
                        elem: '#date',
                        type:'date'
                        ,range: '~' //或 range: '~' 来自定义分割字符
                      });
              let MODULE_PATH = "/admin/role/";
      
              let cols = [
                [  
                    {
                        field: 'id',
                        title: 'ID', 
                        sort: true, 
                        unresize: true,
                        align: 'center'
                    },{
                        field: 'name',
                        title: '角色名称', 
                        sort: true, 
                        unresize: true,
                        align: 'center',
                        templet:function(d){
                            return d.name
                        }
                    },{
                        field: 'addtime',
                        title: '创建时间',
                        unresize: true,
                        align: 'center',
                        width:170
                    },{
                        title: '操作',
                        toolbar: '#options',
                        unresize: true,
                        align: 'center',
                        width: 180
                    }
                ]
              ]
      
              table.render({
                elem: '#dataTable',
                url: MODULE_PATH + 'lst',
                page: true,
                cols: cols,
                          cellMinWidth: 100,
                skin: 'line',
                          type:'post',
                toolbar: '#toolbar',
                          method: 'post'
                ,defaultToolbar: [{
                  title: '刷新',
                  layEvent: 'refresh',
                  icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
              });
      
      
          //监听工具条
        table.on('tool(dataTable)', function(obj){
          var data = obj.data;
          var arr_id=[];
          arr_id.push(data.id);
          console.log(arr_id);
          if(obj.event === 'del'){
            layer.confirm('真的要删除吗', function(index){
              //执行 Ajax 后重载
              $.ajax({
                 type: "POST",
                 url: "{:url('/admin/role/do_del')}",
                 data:{id:arr_id},
                 success: function(msg){
                  if(msg==1){  
                   layer.msg('删除成功', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                      });
                   obj.del();
                   layer.close(index);
                  }else if(msg==2){
                      layer.msg('删除失败,该角色下还有员工', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                  }else{
                    layer.msg('删除失败', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                  }
                 }
              });
              });
          } else if(obj.event === 'edit'){
            var tr = $(obj.tr);
            var id=data.id;
            layer.open({
              type: 2
              ,title: '编辑角色'
              ,content: "{:url('/admin/role/update')}?id="+id
              ,maxmin: true
              ,area:['60%', '80%']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                var iframeWindow = window['layui-layer-iframe'+ index]
                ,submitID = 'LAY-user-front-submit'
                ,submit = layero.find('iframe').contents().find('#'+ submitID);
      
                //监听提交
                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                  var field = data.field; //获取提交的字段
                  
                  //提交 Ajax 成功后，静态更新表格中的数据
                  $.ajax({
                     type: "POST",
                     url: "{:url('/admin/role/do_update')}?id="+id,
                     data: field,
                     success: function(msg){
                       // alert( "Data Saved: " + msg );
                       if(msg==1){
                          layer.msg('修改成功', {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                          });
                      layer.close(index); //关闭弹层
                      table.reload('dataTable');
                       }else if(msg==2){
                        layer.msg('该角色已存在', {
                          offset: '15px'
                          ,icon: 2
                          ,time: 1000
                        });
                       }else{
                        layer.msg('修改失败', {
                          offset: '15px'
                          ,icon: 2
                          ,time: 1000
                        });
                       }
                     }
                  });
                  
      
                });  
                
                submit.trigger('click');
              }
              ,success: function(layero, index){
                
              }
            });
          }else{
              layer.open({
              type: 2,
              title: '权限信息',
              shadeClose: true,
              shade: 0.8,
              area: ['80%', '90%'],
              content: "{:url('/admin/role/detail')}?id="+data.id
            });
          }
        });
          table.on('toolbar(dataTable)', function(obj) {
            if (obj.event === 'add') {
              window.add();
            } else if (obj.event === 'refresh') {
              window.refresh();
            }
          });
           //弹出窗设置 自己设置弹出百分比
              function screen() {
                  if (typeof width !== 'number' || width === 0) {
                  width = $(window).width() * 0.8;
                  }
                  if (typeof height !== 'number' || height === 0) {
                  height = $(window).height() - 20;
                  }
                  return [width + 'px', height + 'px'];
              }
      
              window.add = function() {
                layer.open({
                type: 2
                ,title: '添加角色'
                ,content: "{:url('/admin/role/add')}"
                ,maxmin: true
                ,area:['60%', '80%']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submitID = 'LAY-user-front-submit'
                  ,submit = layero.find('iframe').contents().find('#'+ submitID);
      
                  //监听提交
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                    var field = data.field; //获取提交的字段
                    // layer.alert(JSON.stringify(data.field), {
                    //   title: '最终的提交信息'
                    // })
                    
                    $.ajax({
                     type: "POST",
                     url: "{:url('/admin/role/do_add')}",
                     data: field,
                     success: function(msg){
                       if(msg==1){
                          layer.msg('添加成功', {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                          });
                          table.reload('dataTable'); //数据刷新
                        layer.close(index); //关闭弹层
                       }else if(msg==2){
                        layer.msg('该角色已存在', {
                          offset: '15px'
                          ,icon: 2
                          ,time: 1000
                        });
                       }else{
                          layer.msg('添加失败', {
                          offset: '15px'
                          ,icon: 2
                          ,time: 1000
                        });
                       }
                     }
                  });
                    
                  });  
                  
                  submit.trigger('click');
                }
                // ,cancel: function(index, layero){ 
                //   if(confirm('确定要关闭么')){ //只有当点击confirm框的确定时，该层才会关闭
                //     layer.close(index)
                //   }
                //   return false; 
                // }
              }); 
            }
            window.refresh = function(param) {
              table.reload('dataTable');
            }
          
        });
    </script>
  </body>
</html>